﻿using System;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.InputSystem;

namespace Super
{
	/// <summary>
	/// 编辑器方法拓展，让系统的基类有更多的调用方法
	/// 使用方法：创建对象的实例，通过实例即可调用扩展方法
	/// 不判空，有问题直接报错，方便找到位置修改
	/// </summary>
	public static class EngineExtension
	{
		public static string GetRootPath(this Transform transform)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(transform.name);
			Transform parent = transform.parent;
			while (parent != null)
			{
				sb.Insert(0, parent.name + "/");
				parent = parent.parent;
			}
			return sb.ToString();
		}

		public static Transform GetChild(this Transform transform, string path)
		{
			Transform childTf = transform.Find(path);
			if (childTf == null)
			{
				LogUtil.Log(LogType.Warn, LogCategory.Transform, transform.GetRootPath() + "不存在子路径：" + path);
				return null;
			}
			return childTf;
		}

		public static T GetChildComponent<T>(this Transform transform, string path)
		{
			Transform childTf = transform.GetChild(path);
			if (childTf == null)
			{
				return default(T);
			}

			T component = childTf.GetComponent<T>();
			if (component == null)
			{
				LogUtil.Log(LogType.Warn, LogCategory.Transform, transform.GetRootPath() + "/" + path + "对象不存在组件" + nameof(T));
				return default(T);
			}

			return component;
		}

		public static T TryAddComponent<T>(this Transform transform, string path) where T : Component
		{
			Transform childTf = transform.GetChild(path);
			if (childTf == null)
			{
				return default(T);
			}

			T component = childTf.GetComponent<T>();
			if (component != null)
			{
				return component;
			}
			else
			{
				component = childTf.gameObject.AddComponent<T>();
				return component;
			}
		}

		/// <summary>
		/// ��置按钮监听方法
		/// </summary>
		/// <param name="path"></param>
		/// <param name="action"></param>
		/// <param name="value"></param>
		public static void OnClick(this Transform transform, string path, int value, Action<int> action)
		{
			Button button = transform.GetChildComponent<Button>(path);
			button.onClick.RemoveAllListeners();
			button.onClick.AddListener(() =>
			{
				action(value);
			});
		}

		/// <summary>
		/// 设置按钮监听方法
		/// </summary>
		/// <param name="path"></param>
		/// <param name="action"></param>
		/// <param name="value"></param>
		public static void OnClick(this Transform transform, string path, Action action)
		{
			Button button = transform.GetChildComponent<Button>(path);
			button.onClick.RemoveAllListeners();
			button.onClick.AddListener(() =>
			{
				action();
			});
		}

		/// <summary>
		/// 设置按钮监听方法
		/// </summary>
		/// <param name="path"></param>
		/// <param name="action"></param>
		/// <param name="value"></param>
		public static void OnClick(this Transform transform, string path, Action<Transform, int> action, int index)
		{
			Button button = transform.GetChildComponent<Button>(path);
			button.onClick.RemoveAllListeners();
			button.onClick.AddListener(() =>
			{
				action(transform, index);
			});
		}
		
		public static void OnRightClick(this Transform transform, string path, Action action)
		{
			Transform btnTf = transform.GetChildComponent<Transform>(path);
			BtnRightClick rightClick = btnTf.GetComponent<BtnRightClick>();
			if (rightClick == null)
			{
				rightClick = btnTf.gameObject.AddComponent<BtnRightClick>();
			}
			rightClick.action = action;
		}
		
		public static void OnRightClick(this Transform transform, string path, int value, Action<int> action)
		{
			Transform btnTf = transform.GetChildComponent<Transform>(path);
			BtnRightClick rightClick = btnTf.GetComponent<BtnRightClick>();
			if (rightClick == null)
			{
				rightClick = btnTf.gameObject.AddComponent<BtnRightClick>();
			}
			rightClick.action = () => action(value);
		}

		/// <summary>
		/// 设置异步图片的图片资源
		/// </summary>
		/// <param name="path">子路径</param>
		/// <param name="AName">图片的AddressableName</param>
		public static void SetAsyncImage(this Transform transform, string path, string AName)
		{
			AsyncImage asyncImage = transform.GetChildComponent<AsyncImage>(path);
			if (asyncImage != null)
			{
				asyncImage.SetImageAName(AName);
			}
		}

		/// <summary>
		/// 设置TextMeshPro_UGUI的文本内容
		/// </summary>
		/// <param name="transform"></param>
		/// <param name="path">相对transform的路径</param>
		/// <param name="key"></param>
		public static void SetTMPUGUIByKey(this Transform transform, string path, string key)
		{
			TextMeshProUGUI textMeshProUGUI = transform.GetChildComponent<TextMeshProUGUI>(path);
			if (textMeshProUGUI == null)
			{
				LogUtil.Log(LogType.Error, LogCategory.Transform, transform.GetRootPath() + "/" + path + "对象不存在组件" + nameof(textMeshProUGUI));
				return;
			}

			//保留信息，设置多语言时可以及时刷新
			Lang lang = transform.TryAddComponent<Lang>(path);
			lang.key = key;

			//设置文本
			string langText = LangManager.GetText(key);
			textMeshProUGUI.text = langText;

		}

		public static void SetTMPUGUI(this Transform transform, string path, string text)
		{
			TextMeshProUGUI textMeshProUGUI = transform.GetChildComponent<TextMeshProUGUI>(path);
			if (textMeshProUGUI != null)
			{
				textMeshProUGUI.text = text;
			}
		}

		public static void SetFormatTMPUGUI(this Transform transform, string path, string key, object arg0)
		{
			SetFormatTMPUGUI(transform, path, key, new List<object> { arg0 });
		}

		public static void SetFormatTMPUGUI(this Transform transform, string path, string key, object arg0, object arg1)
		{
			SetFormatTMPUGUI(transform, path, key, new List<object> { arg0, arg1 });
		}

		public static void SetFormatTMPUGUI(this Transform transform, string path, string key, object arg0, object arg1, object arg2)
		{
			SetFormatTMPUGUI(transform, path, key, new List<object> { arg0, arg1, arg2 });
		}
		public static void SetFormatTMPUGUI(this Transform transform, string path, string key, List<object> args)
		{
			TextMeshProUGUI textMeshProUGUI = transform.GetChildComponent<TextMeshProUGUI>(path);
			if (textMeshProUGUI == null)
			{
				LogUtil.Log(LogType.Error, LogCategory.Transform, transform.GetRootPath() + "/" + path + "对象不存在组件" + nameof(textMeshProUGUI));
				return;
			}
			
			//保留信息，设置多语言时可以及时刷新
			LangFormat langFormat = transform.TryAddComponent<LangFormat>(path);
			langFormat.key = key;
			langFormat.args = args;
			
			//设置文本
			string langText = LangManager.GetText(key);
			langText = string.Format(langText, args);
			textMeshProUGUI.text = langText;
		}

		/// <summary>
		/// 设置子角色激活状态
		/// </summary>
		/// <param name="path"></param>
		/// <param name="state"></param>
		public static void SetChildActive(this Transform transform, string path, bool state)
		{
			Transform childTf = transform.GetChild(path);
			if (childTf != null)
			{
				childTf.gameObject.SetActive(state);
			}
		}

		/// <summary>
		/// 子角色激活状态取反
		/// </summary>
		/// <param name="path"></param>
		public static void SetChildToggleState(this Transform transform, string path)
		{
			Transform childTf = transform.GetChild(path);
			if (childTf != null)
			{
				bool state = childTf.gameObject.activeSelf;
				childTf.gameObject.SetActive(!state);
			}
		}
		
		/// <summary>
		/// 获取双项链表的第n个对象
		/// </summary>
		/// <param name="list"></param>
		/// <param name="index"></param>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static T GetIndex<T>(this LinkedList<T> list, int index)
		{
			var node = list.First;
			for (int i = 0; i < index; i++)
			{
				node = node.Next;
			}
			return node.Value;
			
		}
	}
}
